package com.quyunshuo.module.home.view
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.withStyledAttributes
import com.quyunshuo.module.home.R

class SemiCircleProgressBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var progress: Float = 50f
    private var maxProgress: Float = 100f
    private var strokeWidth: Float = 20f
    private var proStrokeWidth: Float = 20f
    private var startColor: Int = Color.parseColor("#FF5722")
    private var endColor: Int = Color.parseColor("#FFC107")
    private var backgroundColor: Int = Color.parseColor("#EEEEEE")
    private var dotColor: Int = Color.WHITE
    private var dotRadius: Float = 8f

    private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val dotPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val backgroundRectF = RectF()
    private val progressRectF = RectF()

    init {
        context.withStyledAttributes(attrs, R.styleable.SemiCircleProgressBar) {
            progress = getFloat(R.styleable.SemiCircleProgressBar_progress, progress)
            maxProgress = getFloat(R.styleable.SemiCircleProgressBar_maxProgress, maxProgress)
            strokeWidth = getDimension(R.styleable.SemiCircleProgressBar_strokeWidth, strokeWidth)
            proStrokeWidth = getDimension(R.styleable.SemiCircleProgressBar_proStrokeWidth, strokeWidth)
            startColor = getColor(R.styleable.SemiCircleProgressBar_startColor, startColor)
            endColor = getColor(R.styleable.SemiCircleProgressBar_endColor, endColor)
            backgroundColor = getColor(R.styleable.SemiCircleProgressBar_backgroundColor, backgroundColor)
            dotColor = getColor(R.styleable.SemiCircleProgressBar_dotColor, dotColor)
            dotRadius = getDimension(R.styleable.SemiCircleProgressBar_dotRadius, dotRadius)
        }

        setupPaints()
    }

    private fun setupPaints() {
        // 背景圆弧画笔
        backgroundPaint.style = Paint.Style.STROKE
        backgroundPaint.strokeWidth = strokeWidth
        backgroundPaint.color = backgroundColor
        backgroundPaint.strokeCap = Paint.Cap.ROUND

        // 进度圆弧画笔
        progressPaint.style = Paint.Style.STROKE
        progressPaint.strokeWidth = proStrokeWidth
        progressPaint.strokeCap = Paint.Cap.ROUND

        // 圆点画笔
        dotPaint.style = Paint.Style.FILL
        dotPaint.color = dotColor
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val width = width.toFloat()
        val height = height.toFloat()

        // 修改1: 优先使用宽度作为基准
        val baseSize = width // 使用宽度作为基础尺寸

        // 修改2: 计算半径时使用 baseSize 而不是 minOf(width, height)
        val bgRadius = baseSize / 2 - strokeWidth / 2

        // 修改3: 计算垂直偏移，使半圆在高度方向居中
        val verticalOffset = (height - baseSize) / 2

        // 设置背景圆弧绘制区域
        backgroundRectF.set(
            width / 2 - bgRadius,
            verticalOffset + (baseSize / 2 - bgRadius),
            width / 2 + bgRadius,
            verticalOffset + (baseSize / 2 + bgRadius)
        )

        // 绘制背景圆弧 (180度半圆)
        canvas.drawArc(backgroundRectF, 180f, 180f, false, backgroundPaint)

        // 设置进度圆弧绘制区域
        progressRectF.set(
            width / 2 - bgRadius,
            verticalOffset + (baseSize / 2 - bgRadius),
            width / 2 + bgRadius,
            verticalOffset + (baseSize / 2 + bgRadius)
        )

        // 设置进度渐变
        val gradient = LinearGradient(
            progressRectF.left, progressRectF.top, progressRectF.right, progressRectF.top,
            startColor, endColor, Shader.TileMode.CLAMP
        )
        progressPaint.shader = gradient

        // 绘制进度圆弧
        val sweepAngle = 180f * (progress / maxProgress)
        canvas.drawArc(progressRectF, 180f, sweepAngle, false, progressPaint)

        // 绘制五个均分圆点 (0%, 25%, 50%, 75%, 100%)
        drawDots(canvas, width / 2, verticalOffset + baseSize / 2, bgRadius)
    }

    private fun drawDots(canvas: Canvas, centerX: Float, centerY: Float, radius: Float) {
        // 计算五个点的角度 (0%, 25%, 50%, 75%, 100%)
        val angles = floatArrayOf(180f, 225f, 270f, 315f, 360f)

        for (angle in angles) {
            val radian = Math.toRadians(angle.toDouble())
            val x = centerX + radius * Math.cos(radian).toFloat()
            val y = centerY + radius * Math.sin(radian).toFloat()
            canvas.drawCircle(x, y, dotRadius, dotPaint)
        }
    }

    // 设置进度
    fun setProgress(progress: Float) {
        this.progress = progress.coerceIn(0f, maxProgress)
        invalidate()
    }

    // 设置最大进度
    fun setMaxProgress(maxProgress: Float) {
        this.maxProgress = maxProgress
        invalidate()
    }

    // 设置进度条粗细
    fun setStrokeWidth(strokeWidth: Float) {
        this.strokeWidth = strokeWidth
        backgroundPaint.strokeWidth = strokeWidth
        progressPaint.strokeWidth = strokeWidth
        invalidate()
    }

    // 设置渐变颜色
    fun setGradientColors(startColor: Int, endColor: Int) {
        this.startColor = startColor
        this.endColor = endColor
        invalidate()
    }

    // 设置圆点颜色
    fun setDotColor(dotColor: Int) {
        this.dotColor = dotColor
        dotPaint.color = dotColor
        invalidate()
    }

    // 设置圆点大小
    fun setDotRadius(dotRadius: Float) {
        this.dotRadius = dotRadius
        invalidate()
    }
}